import { Navigate, useRoutes } from "react-router-dom";
import { RouteObject } from "@/routers/interface";
import Login from "@/views/login/index";
import lazyLoad from "./utils/lazyLoad";
import React from "react";
import { LayoutIndex } from "./constant";
import Home from "@/views/home";

// * 导入所有router
const metaRouters = import.meta.globEager("./modules/*.tsx");

// * 处理路由
export const routerArray: RouteObject[] = [];
Object.keys(metaRouters).forEach((item) => {
	Object.keys(metaRouters[item]).forEach((key: any) => {
		routerArray.push(...metaRouters[item][key]);
	});
});

export const rootRouter: RouteObject[] = [
	{
		key: "loginJump",
		title: "登录跳转",
		path: "/",
		skipAuth: true,
		element: <Navigate to="/login" />,
	},
	{
		path: "/login",
		element: <Login />,
		skipAuth: true,
		title: "登录页",
		key: "login",
	},
	...routerArray,
	{
		path: "/403",
		element: lazyLoad(React.lazy(() => import("@/components/ErrorMessage/403"))),
		skipAuth: true,
		title: "403页面",
		key: "403",
	},
	{
		path: "/404",
		element: lazyLoad(React.lazy(() => import("@/components/ErrorMessage/404"))),
		skipAuth: true,
		title: "404页面",
		key: "404",
	},
	{
		path: "/500",
		element: lazyLoad(React.lazy(() => import("@/components/ErrorMessage/500"))),
		skipAuth: true,
		title: "500页面",
		key: "500",
	},
	{
		key: "other",
		title: "其他路由",
		skipAuth: true,
		path: "*",
		element: <Navigate to="/404" />,
	},
];

const Router = () => {
	const routes = useRoutes(rootRouter);
	return routes;
};

export default Router;
